package com.hspedu.single_;

/**
 * 饿汉式
 */
public class SingleTon01 {
    public static void main(String[] args) {

        GirlFriend instance = GirlFriend.getInstance();
        System.out.println(instance);

        GirlFriend instance2 = GirlFriend.getInstance();
        System.out.println(instance2);

        System.out.println(instance == instance2);
    }
}

//只能创建一个 GirlFriend 对象
//【单例模式-饿汉式】
class GirlFriend {

    private String name;
    //2.在类的内部直接创建
    //为了能在静态方法中返回 gf 对象，需要将其修饰为 static
    private static GirlFriend gf = new GirlFriend("小红");

    //1.构造器私有化
    private GirlFriend(String name) {
        this.name = name;
    }

    //3.提供一个公共的静态方法，返回 gf 对象
    public static GirlFriend getInstance() {
        return gf;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                '}';
    }
}
